home *** CD-ROM | disk | FTP | other *** search
/ Oh!X 2001 Spring / Oh!X 2001 Spring Special CD-ROM (Japan) (Track 1).bin / VC / layered / layered.cpp next >
Encoding:
C/C++ Source or Header  |  2000-08-02  |  4.2 KB  |  154 lines

  1. // layered.cpp : アプリケーション用のエントリ ポイントの定義
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "layeredwnd.h"
  6.  
  7. #define APPNAME "LayeredWindow"
  8. static char szAppName[] = APPNAME;
  9. static char szTitle[]   = APPNAME;
  10.  
  11. LayeredWnd *playeredWnd;
  12.  
  13. HINSTANCE    hInst;    // USER32.DLL のインスタンスハンドル
  14. //    SetLayeredWindowAttributes のアドレス
  15. BOOL (WINAPI *_SetLayeredWindowAttributes)(HWND,COLORREF,BYTE,DWORD);
  16.  
  17. int APIENTRY WinMain(HINSTANCE hInstance,
  18.                      HINSTANCE hPrevInstance,
  19.                      LPSTR     lpCmdLine,
  20.                      int       nCmdShow )
  21. {
  22.      // TODO: この位置にコードを記述してください。
  23.     OSVERSIONINFO versioninfo;
  24.     versioninfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  25.     GetVersionEx( &versioninfo );
  26.     if( versioninfo.dwMajorVersion<5 ){
  27.         MessageBox( NULL, "このバージョンのWindowsでは動作しません。", "LayeredWindow",MB_OK|MB_ICONHAND ); 
  28.         return FALSE;
  29.     }
  30.     if( !InitInstance( hInstance ) ){
  31.         MessageBox( NULL, "アプリケーションの初期化に失敗しました。", "LayeredWindow", MB_OK|MB_ICONHAND );
  32.         return FALSE;
  33.     }
  34.  
  35.     MSG msg;
  36.     while( GetMessage( &msg, NULL, 0, 0 ) ){
  37.         TranslateMessage( &msg );    // Translates virtual key codes.
  38.         DispatchMessage( &msg );    // Dispatches message to window.
  39.     }
  40.     ExitInstance();
  41.     return msg.wParam;    // Returns the value from PostQuitMessage.
  42. }
  43.  
  44. BOOL InitInstance( HINSTANCE hInstance )
  45. {
  46.     // SetLayeredWindowAttributes のエクスポートアドレスの取得
  47.     hInst = LoadLibrary( "USER32.DLL" );
  48.     if( hInst ){
  49.         _SetLayeredWindowAttributes = (BOOL(WINAPI*)(HWND,COLORREF,BYTE,DWORD))
  50.             GetProcAddress( hInst, "SetLayeredWindowAttributes" );
  51.     }
  52.     if( _SetLayeredWindowAttributes==NULL ) return FALSE;
  53.  
  54.     playeredWnd = new LayeredWnd( hInstance );
  55.     HWND hWnd = playeredWnd->Create();
  56.     return hWnd?TRUE:FALSE;
  57. }
  58.  
  59. void ExitInstance()
  60. {
  61.     if( playeredWnd ) delete playeredWnd;
  62.     if( hInst ) FreeLibrary( hInst );
  63. }
  64.  
  65. LONG APIENTRY MainWndProc( HWND hWnd, UINT message, UINT wParam, LONG lParam )
  66. {
  67.     if( message==WM_NCCREATE ){
  68.         CREATESTRUCT *pcs = (CREATESTRUCT*)lParam;
  69.         SetWindowLong( hWnd, GWL_USERDATA, (LONG)pcs->lpCreateParams );
  70.     }
  71.     LayeredWnd *pWnd = (LayeredWnd*)GetWindowLong( hWnd, GWL_USERDATA );
  72.     if( pWnd==NULL ){
  73.         return DefWindowProc( hWnd, message, wParam, lParam );
  74.     } else {
  75.         return pWnd->WndProc( hWnd, message, wParam, lParam );
  76.     }
  77.     return 0;
  78. }
  79.  
  80. //////////////////////////////////////////////////////////////////////
  81. // LayeredWnd クラス
  82. //////////////////////////////////////////////////////////////////////
  83.  
  84. //////////////////////////////////////////////////////////////////////
  85. // 構築/消滅
  86. //////////////////////////////////////////////////////////////////////
  87.  
  88. LayeredWnd::LayeredWnd( HINSTANCE hInstance )
  89. {
  90.     WNDCLASSEX wc;
  91.     wc.cbSize = sizeof( WNDCLASSEX );
  92.     wc.style = 0;
  93.     wc.lpfnWndProc = (WNDPROC)MainWndProc;
  94.     wc.cbClsExtra = 0;
  95.     wc.cbWndExtra = 0;
  96.     wc.hInstance = hInstance;
  97.     wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
  98.     wc.hCursor = LoadCursor( NULL, IDC_ARROW );
  99.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  100.     wc.lpszMenuName = NULL;
  101.     wc.lpszClassName = szAppName;
  102.     wc.hIconSm = NULL;
  103.     RegisterClassEx( &wc );
  104.     m_hInstance = hInstance;
  105. }
  106.  
  107. LayeredWnd::~LayeredWnd()
  108. {
  109.  
  110. }
  111.  
  112. HWND LayeredWnd::Create()
  113. {
  114.     CREATESTRUCT cs;
  115.     cs.lpCreateParams = this;
  116.     cs.hInstance = m_hInstance;
  117.     cs.hMenu = NULL;
  118.     cs.hwndParent = NULL;
  119.     cs.cy = CW_USEDEFAULT;
  120.     cs.cx = CW_USEDEFAULT;
  121.     cs.y = CW_USEDEFAULT;
  122.     cs.x = CW_USEDEFAULT;
  123.     cs.style = WS_OVERLAPPEDWINDOW|WS_VISIBLE;
  124.     cs.lpszName = szTitle;
  125.     cs.lpszClass = szAppName;
  126.     cs.dwExStyle = WS_EX_LAYERED;    // レイヤードウィンドウ
  127.     m_hWnd = CreateWindowEx( cs.dwExStyle, cs.lpszClass, cs.lpszName, cs.style,
  128.         cs.x, cs.y, cs.cx, cs.cy, cs.hwndParent, cs.hMenu, cs.hInstance, cs.lpCreateParams );
  129.     return m_hWnd;
  130. }
  131.  
  132. LRESULT LayeredWnd::WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
  133. {
  134.     switch( message ){
  135.     case WM_CREATE:
  136.         OnCreate( hWnd );
  137.         return 0;
  138.     case WM_DESTROY:
  139.         OnDestroy();
  140.         PostQuitMessage( 0 );
  141.         return 0;
  142.     }
  143.     return DefWindowProc( hWnd, message, wParam, lParam );
  144. }
  145.  
  146. void LayeredWnd::OnCreate( HWND hWnd )
  147. {
  148.     _SetLayeredWindowAttributes( hWnd, 0, 128, LWA_ALPHA );
  149. }
  150.  
  151. void LayeredWnd::OnDestroy()
  152. {
  153. }
  154.